package problem49;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;


//49.字母异位词分组
//https://leetcode.cn/problems/group-anagrams/description/

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String, List<String>> map = new HashMap<>();
        for(String s : strs) {
            char[] str = s.toCharArray();
            Arrays.sort(str);
            String t = new String(str);
            if(map.containsKey(t)) {
                map.get(t).add(s);
            }else {
                List<String> list = new ArrayList<>();
                list.add(s);
                map.put(t, list);
            }
        }
        List<List<String>> ret = new ArrayList<>();
        for(List<String> list : map.values()) {
            ret.add(list);
        }
        return ret;
    }
}